package cn.HuanG.org.controller;

import cn.HuanG.org.domain.Employee;
import cn.HuanG.org.domain.Product;
import cn.HuanG.org.query.ProductQuery;
import cn.HuanG.org.service.IProductService;
import cn.HuanG.util.AjaxResult;
import cn.HuanG.util.LoginContext;
import cn.HuanG.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 员工表controller模块
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping
    public List<Product> getAll(){
        return  productService.queryAll();
    }
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product, HttpServletRequest request){
        try {
            if(product.getId()==null){
                //获取当前登录用户，再获取对应的点铺
                Employee employee = (Employee) LoginContext.getCurrent(request);
                productService.save(product);
                System.out.println("错误打印"+product.getOffsaletime());
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @GetMapping("/{id}") //查询，区分通过uri（id占位符）
    public Product findById(@PathVariable("id") Long id){
        return productService.queryById(id);
    }
    @PostMapping  //分页专用
    public PageInfo<Product> page(@RequestBody ProductQuery query){
        return productService.queryPage(query);

    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody Long[] ids){
        try {
            System.out.println(ids);
            productService.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @GetMapping("/detail/{id}")
    public Product detail(@PathVariable Long id){
        Product product = productService.getDetail(id);
        return  product;
    }
}

























